var app = getApp();
import { showActionSheet } from '../../utils/camera.js';
Page({
    data: {
        main:0,
        tabs: [{ "name": "最新", "type": 'new' }, { "name": "最热", "type": "hot" }],
        activeIndex:0,
        tabBar:[{ "name": "留言板",'icon':'/images/liu.png','url':'../home/home','selectIcon':'/images/mine.png' },{ "name": "我的",'icon':'/images/mine.png','url':'../mine/mine','selectIcon':'/images/liu.png' }],
        moveIndex:0,
        start:false,
        newList:[],
        info:{},
        hotList:[],
        activeList:[],
        activeImg:null,
        ineedmt:0,
        flag:false,
        brandInfo:{},
        tapNum:[],
        brandList:[],
        brandListStatus:true,
        liuyan:true,
        src:'/images/shouqi.png'
    },
    clickNum:function(e){
        var self=this
        if(e.currentTarget.id==1){
            // app.showToast()
            wx.removeStorageSync('hotStroList')
            self.hotFun()
        }else{
            // app.showToast()
            wx.removeStorageSync('newStroList')
            self.newFun()
        }
        self.setData({
            activeIndex: e.currentTarget.id,
            main:e.currentTarget.id
        })
    },
    clickTab:function(e){
        var self=this
        var list=self.data.tabBar
        var index=e.currentTarget.id
        self.setData({
            moveIndex:index ,
        })
        wx.redirectTo({
            url: list[index].url
        })
        
        
  },
  camera: function (e) {
    var self = this
    showActionSheet({ name: 'headimg', isOne: 1, type: { "type": 'headimg' } }).then(data => {
      var user = data.user
      wx.setStorageSync('wechat_service_user', user)
      app.globalData.userInfo = user
      self.setData({
        user: user
      })
    })
  },
  onPullDownRefresh: function(){
    if(this.data.brandListStatus){
        var self=this
        wx.redirectTo({
            url: '/pages/brand/brand',
        })
        wx.stopPullDownRefresh()
    }
    
  },
  onReady:function(){
      var that=this
      var value=wx.getStorageSync('userInfo')
      that.setData({
          info:value
      })
  },
  hotFun:function(){
    var value=wx.getStorageSync('userInfo')
    var brand=wx.getStorageSync('brandId')
    var that=this
    var hotStroList=wx.getStorageSync('hotStroList')
    if(hotStroList!=null&& hotStroList.length!=0){
        that.setData({
            hotList:hotStroList
        })
    }else{
        wx.request({
            url: 'https://www.hibigpc.com/getInfoByBrandHot',
            method:'post',
            data: {
                brandId:brand,
                uuid:value.uuid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res)
                var cardList=res.data.dataWrapper.tbLatestWords
                wx.setStorageSync('hotStroList', cardList)
                that.setData({
                    hotList:cardList
                })
            }
        })
    }
  },
  newFun:function(){
      var value=wx.getStorageSync('userInfo')
      var brand=wx.getStorageSync('brandId')
      var that=this
      var newStroList=wx.getStorageSync('newStroList')
      if(newStroList!=null&& newStroList.length!=0){
            that.setData({
                newList:newStroList
            })
      }else{
            wx.request({
            url: 'https://www.hibigpc.com/getInfoByBrandLatest',
            method:'post',
            data: {
                brandId:brand,
                uuid:value.uuid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res)
                var cardList=res.data.dataWrapper.tbLatestWords
                wx.setStorageSync('newStroList', cardList)
                that.setData({
                    newList:cardList
                })
            }
        })
      }
    
    
  },
  savePraise:function(e){
      var value=wx.getStorageSync('userInfo')
      var that=this
      that.setData({
          ineedmt:e.target.dataset.zanindex,
          flag:e.target.dataset.zanflag
      })
      if(e.target.dataset.zanflag=='true'){
            wx.request({
            url: 'https://www.hibigpc.com/savePraise',
            data: {
                id:e.currentTarget.id,
                uuid:value.uuid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                // console.log(res.data)
                if(res.data.status=="0"){
                    if(that.data.activeIndex==0){
                        var newList=that.data.newList
                        newList[that.data.ineedmt].deleted=that.data.flag
                        newList[that.data.ineedmt].praisenum=newList[that.data.ineedmt].praisenum+1
                        that.setData({
                            newList:newList
                        })
                    }else{
                        var hotList=that.data.hotList
                        hotList[that.data.ineedmt].deleted=that.data.flag
                        hotList[that.data.ineedmt].praisenum=hotList[that.data.ineedmt].praisenum+1
                        that.setData({
                            hotList:hotList
                        })
                    }
                }else{
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel:false
                    })
                }
            }
        }) 
      }
     
  },
  saveRead:function(e){
    var self=this
    var readList=wx.getStorageSync('saveReadList')
    if(readList.length!=0){
        for(var i=0;i<readList.length;i++){
            if(readList[i].indexOf(e.currentTarget.id)!=-1){
                return;
            }
        }
    }
    wx.request({
        url: 'https://www.hibigpc.com/saveReadNum',
        data: {
            id:e.currentTarget.id
        },
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            self.data.tapNum.push(e.currentTarget.id)
            wx.setStorageSync('saveReadList', self.data.tapNum)
            if(self.data.activeIndex==0){
                wx.removeStorageSync('newStroList');
                self.newFun()
            }else{
                wx.removeStorageSync('hotStroList');
                self.hotFun()
            }
        }
    }) 
  },
  onLoad:function(options){
    var that=this
    if(options.brandId){
        // console.log(options.brandId)
        wx.removeStorageSync('newStroList');
        wx.removeStorageSync('hotStroList');
        var keyId=options.brandId
        wx.setStorageSync('brandId', keyId)
        // app.showToast()
        that.newFun()
        that.getBrandSync()
        var accessTokenVal=options.accessToken
        wx.request({
          url:'https://www.hibigpc.com/scam',
          method:'post',
          data: {
              brandId:keyId,
              accessToken:accessTokenVal
          },
          header: {
            'content-type':'application/x-www-form-urlencoded'
          },
          success: function(res) {
              
          }
      })

    }else{
        var brand=wx.getStorageSync('brandId')
        if(brand){
            var keyId=brand
            wx.setStorageSync('brandId', keyId)
            that.getBrandSync()
            that.newFun()
        }else{
            that.getBrandList()
        }
        
    }
    
    
  },
  getBrandList:function(){
    var that=this
    var brandId=wx.getStorageSync('brandId')
    var long1=wx.getStorageSync('long')
    var lat1=wx.getStorageSync('lat')
    wx.request({
        url:'https://www.hibigpc.com/showList',
        method:'post',
        data: {
            long1:long1,
            lat1:lat1,
            brandId:brandId
        },
        header: {
            'content-type':'application/x-www-form-urlencoded'
        },
        success: function(res) {
           console.log(res.data)
           var brandListInfo=res.data.dataWrapper.brands
           if(brandListInfo.length==1){
                wx.setStorageSync('brandId',brandListInfo[0].id )
                that.getBrandSync()
                that.newFun()
           }else{
                that.setData({
                    brandList:brandListInfo,
                    brandListStatus:false
                })
           }
           
        }
    })
  },
  backIndex:function(e){
    // console.log(e.currentTarget.id)
    wx.removeStorageSync('newStroList')
    wx.removeStorageSync('hotStroList')
    var that=this
    wx.setStorageSync('brandId',e.currentTarget.id)
    that.getBrandSync()
    that.newFun()
    that.setData({
        brandListStatus:true
    })
  },
  getBrandSync:function(){
    var that=this
    var brandId=wx.getStorageSync('brandId')
    wx.request({
        url:'https://www.hibigpc.com/tbBrand/get',
        method:'post',
        data: {
            key:brandId
        },
        header: {
            'content-type':'application/x-www-form-urlencoded'
        },
        success: function(res) {
            var getInfo=res.data.dataWrapper.tbBrand
            wx.setStorageSync('brand', getInfo)
            that.setData({
                brandInfo:getInfo
            })
        }
    })
  },
  checkMail:function(){
      var that=this
    //   that.camera()
        wx.getLocation({
            type: 'wgs84',
            success: function(res) {
                // console.log(res)
                var latitude=res.latitude
                var longitude=res.longitude
                var brand=wx.getStorageSync('brand')
                var long2=brand.location.split(',')[0]
                var lat2=brand.location.split(',')[1]
                wx.request({
                    url: 'https://www.hibigpc.com/valideauthority',
                    method:'post',
                    data: {
                        long1:longitude,
                        lat1:latitude,
                        long2:long2,
                        lat2:lat2,
                    },
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    success: function(res) {
                        // that.camera()
                        console.log(res.data)
                        if(res.data.status=='-1'){
                            wx.showModal({
                                title: '提示',
                                content: res.data.msg,
                                showCancel:false
                            })
                        }else{
                            var dataList=res.data.dataWrapper.list
                            if(dataList==undefined){
                                that.camera()
                            }else{
                                that.setData({
                                    brandList:dataList,
                                    brandListStatus:false,
                                    liuyan:false
                                })
                            } 
                        }
                        
                    }
                })
            }
        })  
    },
    onShareAppMessage: function () {
        var brandId=wx.getStorageSync('brandId')
        return {
        title: '到此一玩小程序',
        path: '/pages/home/home?brandId='+brandId,
        success: function(res) {
            // 分享成功
        },
        fail: function(res) {
            // 分享失败
        }
        }
    },
    close:function(){
        var that=this
        that.setData({
           brandListStatus:true 
        })
    },
    scaleImg:function(e){
      console.log(e)
      var arr=[]
      var imgUrl = e.currentTarget.dataset.url
      arr.push(imgUrl)
      wx.previewImage({
        current: imgUrl, // 当前显示图片的http链接
        urls: arr // 需要预览的图片http链接列表
      })
    },
    open: function (id) {
      var that=this
        wx.showActionSheet({
          itemList: ['删除'],
          success: function (res) {
            if (!res.cancel) {
              wx.request({
                url: 'https://www.hibigpc.com/tbComment/del',
                method: 'post',
                data: {
                  key: id
                },
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  console.log(res)
                  if (res.data.status == 0) {
                    if (that.data.activeIndex == 0) {
                      wx.removeStorageSync('newStroList');
                      that.newFun()
                    } else {
                      wx.removeStorageSync('hotStroList');
                      that.hotFun()
                    }
                  }

                }
              })
            }
          }
        });
    },
    pinlun:function(e){
      var that=this
      var wordsId = e.currentTarget.dataset.url;
      var id = e.currentTarget.dataset.con
      var commentUserId = e.currentTarget.dataset.commentuserid
      var reviewUserId = e.currentTarget.dataset.reviewuserid
      console.log(commentUserId)
      if (commentUserId == undefined){
        commentUserId = wx.getStorageSync("userInfo").uuid;
        reviewUserId:''
      }else{
        if (commentUserId == wx.getStorageSync("userInfo").uuid){
          that.open(id)
          return;
        }else{
          if (reviewUserId == undefined) {
            reviewUserId = wx.getStorageSync("userInfo").uuid;
          }else{
            if (reviewUserId == wx.getStorageSync("userInfo").uuid){
              that.open(id)
              return;
            }
          }
        }
      }
      var data={
        wordsId: wordsId,
        commentUserId: commentUserId,
        id:1,
        reviewUserId: reviewUserId
      }
      wx.setStorageSync('data', data)
      wx.navigateTo({
          url: '/pages/pinlun/pinlun'
        })
      },
      isZhan:function(e){

      }
}) 